package com.ce.webframework.sec.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.ce.component.authorization.external.service.IAuthFunctionEntityManagementService;
import com.ce.component.authorization.model.ComAuthEntityDO;
import com.ce.component.authorization.model.state.AuthEntityState;
import com.ce.pms.authorization.filter.PmsAuthenticateFilter;
import com.ce.webframework.sec.service.ISecMsgTipsService;
import com.ce.webframework.sec.vo.MsgGlobal;
import com.ce.webframework.sec.vo.SecMsgTipsVO;

/**
*
* <p>版权所有：中企动力
* <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
* <p>功能描述：处理项目初始化操作的PlugIn
*
* @author kuanglingxuan
* Created on 2007-9-25
*/
public class InitPlugIn implements PlugIn{

	public  static  final String  APPLICATION_ALL_AUTH = "allAuth";

	private ISecMsgTipsService secMsgTipsService;

	public void setSecMsgTipsService(ISecMsgTipsService secMsgTipsService){
		this.secMsgTipsService = secMsgTipsService;
	}

	private ServletContext context;

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void init(ActionServlet servlet, ModuleConfig config) throws ServletException {

		context = servlet.getServletContext();
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
		IAuthFunctionEntityManagementService authFunctionEntityManagementService=(IAuthFunctionEntityManagementService)wac.getBean(PmsAuthenticateFilter.BIZ_SYSTEM__ALLRIGHT_SERVICE_KEY);
		/*读取所有的受控权限begin*/
		Map<String,String> allMap=new HashMap();
		List<ComAuthEntityDO> comAuthEntityList=authFunctionEntityManagementService.searchFunctionEntities(null, null, AuthEntityState.getInstance(1),null, null);
		for(ComAuthEntityDO tempObject:comAuthEntityList){
			allMap.put(tempObject.getName().trim(), tempObject.getName().trim());
		}
		comAuthEntityList=null;

		wac.getServletContext().setAttribute(APPLICATION_ALL_AUTH, allMap);
		/*读取所有的受控权限end*/


		setSecMsgTipsService((ISecMsgTipsService)wac.getBean("secMsgTipsService"));

		TimerTask tt = new TimerTask(this, "initDailyTimers", null);
		tt.initDaily();


	}

	public void initDailyTimers(){

		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		List<SecMsgTipsVO> times =  secMsgTipsService.getMsgTipForTimer();

		MsgGlobal mg =(MsgGlobal) context.getAttribute("msgGlobal");

		if(mg == null){//如果启动系统mg对象为空，就从数据库查出用户未读消息的用户编码
			mg = new MsgGlobal();
			mg.setUserCodeList(secMsgTipsService.getCodesNoReadMsg());
			context.setAttribute("msgGlobal", mg);
		}
		try {

			for(SecMsgTipsVO vo : times){
				CommonUtil.addMsgTimer(context,this,"process",vo);

			}
		} catch (Exception e) {

			e.printStackTrace();
		}
		secMsgTipsService.setServletContext(context);
	}

	public void process(SecMsgTipsVO vo){

		List<Long> ids = new ArrayList<Long>();
		ids.add(vo.getMsgTipId());
		secMsgTipsService.updateReadFlag(ids, '0');//更新为未读

		CommonUtil.addMsgGlobal(context, vo.getReceiverCode());
	}


}
